#########################################################################################
# streflop

# IMPORTANT: should come BEFORE finding packages
IF(WANT_STREFLOP)
	ADD_SUBDIRECTORY(sources/streflop)
ENDIF()

#########################################################################################
# common libraries

SET(EXTERNAL_LIBS "")
SET(TARGET_NAME "libmegaglest")

IF(BUILD_MEGAGLEST_MODEL_VIEWER OR BUILD_MEGAGLEST_MAP_EDITOR OR BUILD_MEGAGLEST)
	MESSAGE(STATUS "Will try to build MegaGlest shared library")

	INCLUDE (CheckIncludeFiles)

	#Game options
	option(WANT_STATIC_LIBS "builds as many static libs as possible" OFF)
	option(ENABLE_FRIBIDI "Enable FriBIDi support" ON)
	option(FORCE_LUA_5_1 "Force looking for LUA 5.1" OFF)
	option(FORCE_LUA_5_2 "Force looking for LUA 5.2" OFF)

	IF(WIN32)
		SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH}
					${PROJECT_SOURCE_DIR}/source/win32_deps/lib
					${PROJECT_SOURCE_DIR}/source/win32_deps/xerces-c-src_2_8_0/lib
					${PROJECT_SOURCE_DIR}/source/win32_deps/wxWidgets-2.8.10/lib)

		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/include)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/openal-soft-1.12.854/include)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/openal-soft-1.12.854)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/xerces-c-src_2_8_0/include)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/libogg-1.2.1/include)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/lua-5.1/src)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/jpeg-8b)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/lpng141)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/zlib-1.2.5)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/curl-7.21.3/include)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/SDL-1.2.x/include)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/wxWidgets-2.8.10/include)
				       
		link_directories(${PROJECT_SOURCE_DIR}/source/win32_deps/lib)
		link_directories(${PROJECT_SOURCE_DIR}/source/win32_deps/xerces-c-src_2_8_0/lib)
		link_directories(${PROJECT_SOURCE_DIR}/source/win32_deps/wxWidgets-2.8.10/lib)

		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/win32_deps/include)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/shared_lib/include/platform/posix)
		INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/shared_lib/include/platform/win32)
	ENDIF()

	FIND_PACKAGE(CURL REQUIRED)
	INCLUDE_DIRECTORIES(${CURL_INCLUDE_DIRS})
	IF(UNIX)
		SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${CURL_LIBRARIES})
	ENDIF()
	# check libcurl version #
	IF(WIN32)
		file (READ ${PROJECT_SOURCE_DIR}${CURL_INCLUDE_DIR}/curl/curlver.h CURL_VERSION_H_CONTENTS)
	ELSE()
		file (READ ${CURL_INCLUDE_DIR}/curl/curlver.h CURL_VERSION_H_CONTENTS)
	ENDIF()

	string (REGEX MATCH "LIBCURL_VERSION_MAJOR[ \t]+([0-9]+)" LIBCURL_VERSION_MJ ${CURL_VERSION_H_CONTENTS})
	string (REGEX MATCH "([0-9]+)" LIBCURL_VERSION_MJ ${LIBCURL_VERSION_MJ})
	string (REGEX MATCH "LIBCURL_VERSION_MINOR[ \t]+([0-9]+)" LIBCURL_VERSION_MI ${CURL_VERSION_H_CONTENTS})
	string (REGEX MATCH "([0-9]+)" LIBCURL_VERSION_MI ${LIBCURL_VERSION_MI})
	string (REGEX MATCH "LIBCURL_VERSION_PATCH[ \t]+([0-9]+)" LIBCURL_VERSION_PT ${CURL_VERSION_H_CONTENTS})
	string (REGEX MATCH "([0-9]+)" LIBCURL_VERSION_PT ${LIBCURL_VERSION_PT})
	set (CURL_MAJOR_VERSION ${LIBCURL_VERSION_MJ})
	set (CURL_MINOR_VERSION ${LIBCURL_VERSION_MI})
	set (CURL_PATCH_VERSION ${LIBCURL_VERSION_PT})
	include_regular_expression("^.*$")
	# Setup package meta-data
	set(CURL_VERSION ${CURL_MAJOR_VERSION}.${CURL_MINOR_VERSION}.${CURL_PATCH_VERSION})
	message(STATUS "curl version=[${CURL_VERSION}]")
	#IF( ${CURL_VERSION} VERSION_LESS 7.21.0)
	IF( ${CURL_VERSION} VERSION_LESS 7.16.4)
		message(STATUS "(please visit http://curl.haxx.se/libcurl/ to find a newer version)")	
		#message(FATAL_ERROR " CURL version = [${CURL_VERSION}] we require AT LEAST [7.21.0]")
		message(FATAL_ERROR " CURL version = [${CURL_VERSION}] we require AT LEAST [7.16.4]")
	ENDIF()

	FIND_PACKAGE(SDL REQUIRED)
	INCLUDE_DIRECTORIES(${SDL_INCLUDE_DIR})
	IF(UNIX)
		SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${SDL_LIBRARY})
	ENDIF()

	FIND_PACKAGE(OpenGL REQUIRED)
	INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR})
	IF(UNIX)
		SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${OPENGL_LIBRARY})
	ENDIF()


        IF(WANT_XERCES)
	        FIND_PACKAGE(XercesC REQUIRED)
	        INCLUDE_DIRECTORIES(${XERCESC_INCLUDE_DIR})
	        IF(UNIX)
		        SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${XERCESC_LIBRARY})
	        ENDIF()
        ENDIF()

	FIND_PACKAGE(OpenAL REQUIRED)
	INCLUDE_DIRECTORIES(${OPENAL_INCLUDE_DIR})
	IF(UNIX)
		SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${OPENAL_LIBRARY})
	ENDIF()

        FIND_PACKAGE(LUA REQUIRED)
        INCLUDE_DIRECTORIES(${LUA_INCLUDE_DIR})
        IF(UNIX)
	        SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${LUA_LIBRARIES} ${CMAKE_DL_LIBS})
        ENDIF()

	IF(WANT_STATIC_LIBS)
		OPTION(JPEG_STATIC "Set to ON to link your project with static library (instead of DLL)." ON)
		OPTION(PNG_STATIC "Set to ON to link your project with static library (instead of DLL)." ON)
	ENDIF()
	IF(JPEG_STATIC)
		SET(JPEG_NAMES libjpeg.a ${JPEG_NAMES})
	ELSE()
		SET(JPEG_NAMES libjpeg ${JPEG_NAMES})
	ENDIF()

	FIND_PACKAGE(JPEG REQUIRED)
	INCLUDE_DIRECTORIES(${JPEG_INCLUDE_DIR})
	IF(UNIX)
		SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${JPEG_LIBRARY})
	ENDIF()
	
	IF(PNG_STATIC)
		SET(PNG_NAMES libpng.a ${PNG_NAMES})
	ELSE()
		SET(PNG_NAMES libpng ${PNG_NAMES})
	ENDIF()
	FIND_PACKAGE(PNG REQUIRED)
	INCLUDE_DIRECTORIES(${PNG_INCLUDE_DIR})
	IF(UNIX)
		SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${PNG_LIBRARY})
	ENDIF()

        OPTION(WANT_FONTCONFIG "use the library fontconfig" ON)
	IF(WANT_FONTCONFIG)
	        FIND_PACKAGE(FontConfig)
	        IF(FONTCONFIG_FOUND)
                    MESSAGE(STATUS "**NOTE: FontConfig support was detected and enabled.")
	            SET(HAVE_FONTCONFIG 1)
                    ADD_DEFINITIONS(-DHAVE_FONTCONFIG)

	            INCLUDE_DIRECTORIES( ${FONTCONFIG_INCLUDE_DIR} ) 
	            SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${FONTCONFIG_LIBRARIES} )
	        ENDIF(FONTCONFIG_FOUND)
        ENDIF()

	OPTION(USE_FTGL "Use FTGL for on-screen fonts (found on your system)" ON)
	if(USE_FTGL)
		ADD_DEFINITIONS(-DUSE_FTGL)

		find_package( Freetype REQUIRED)
		include_directories( ${FREETYPE_INCLUDE_DIRS} )
	
	endif(USE_FTGL)

	FIND_PACKAGE(FTGL)


	IF(USE_FTGL)
		if (FTGL_FOUND)
		        MESSAGE(STATUS "**NOTE: FTGL font support was detected and enabled.")
		else()
		        MESSAGE(STATUS 
			"**NOTE: Warning! FTGL has been enabled but not detected. Your compilation will probably break. Turn off FTGL support by setting USE_FTGL to false, or visit http://ftgl.wiki.sourceforge.net/ for help on installing FTGL.")
		endif()
	
		ADD_DEFINITIONS(-DUSE_FTGL)
	
		#set(FTGL_INCUDE_DIR ${FTGL_INCLUDE_DIR} ${CMAKE_INCLUDE_PATH} ${OPENGL_INCLUDE_DIR})
		#set(FTGL_LINK_DIRS ${FTGL_LIBRARY_DIRS} )
		#set(FTGL_LINK_TARGETS ftgl freetype)
	
		INCLUDE_DIRECTORIES(${FTGL_INCLUDE_DIR})
		SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${FTGL_LIBRARY})

	ELSE()
		IF(FTGL_FOUND)
		        MESSAGE(STATUS "**NOTE:  Warning: FTGL font support was detected but NOT enabled. You can enable it by setting USE_FTGL=true")
		ELSE()
		        MESSAGE(STATUS "**NOTE:  Warning: FTGL font support was not detected. Visit http://ftgl.wiki.sourceforge.net/ for help on installing FTGL.")
		ENDIF()
	
	ENDIF(USE_FTGL)

	FIND_PACKAGE(Freetype REQUIRED)
	INCLUDE_DIRECTORIES(${FREETYPE_INCLUDE_PATH})
	IF(UNIX)
		SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${FREETYPE_LIBRARY})
	ENDIF()

	FIND_PACKAGE(GLEW REQUIRED)
	INCLUDE_DIRECTORIES(${GLEW_INCLUDE_PATH})
	IF(UNIX)
		SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${GLEW_LIBRARY})
	ENDIF()

        SET(LIBVLC_MIN_VERSION "1.1.0")
	FIND_PACKAGE(LIBVLC)

        MESSAGE(STATUS "LIBVLC version: ${LIBVLC_VERSION} WITH_VLC macro: ${WITH_VLC}")

        IF(LIBVLC_FOUND AND WITH_VLC AND NOT LIBVLC_VERSION STRLESS "${LIBVLC_MIN_VERSION}")
                MESSAGE(STATUS "**NOTE: LIBVLC found so videos are supported.")
	        INCLUDE_DIRECTORIES(${LIBVLC_INCLUDE_DIR})
	        IF(UNIX)
		        SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${LIBVLCCORE_LIBRARY} ${LIBVLC_LIBRARY})
                        ADD_DEFINITIONS(-DHAS_LIBVLC)
	        ENDIF()
		IF(LIBVLC_VERSION_PRE_V2)
			ADD_DEFINITIONS(-DLIBVLC_VERSION_PRE_2)
		ENDIF()
		IF(LIBVLC_VERSION_PRE_1_1_13)
			ADD_DEFINITIONS(-DLIBVLC_VERSION_PRE_1_1_13)
		ENDIF()
		
        ELSE()
                MESSAGE(STATUS "**WARNING: LIBVLC NOT found so videos are NOT supported.")
                
                IF(WITH_VLC)
	                MESSAGE(STATUS "**WARNING: LIBVLC NOT found so videos are NOT supported.")
		ELSE()
			MESSAGE(STATUS "LIBVLC disabled so videos are NOT supported.")
		ENDIF()
        ENDIF()


	find_package( FriBiDi )
	if(ENABLE_FRIBIDI AND FRIBIDI_LIBRARIES)
	        add_definitions(-DHAVE_FRIBIDI)

        	include_directories( ${FRIBIDI_INCLUDE_DIR} )
                SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${FRIBIDI_LIBRARIES})

	elseif(ENABLE_FRIBIDI AND NOT FRIBIDI_LIBRARIES)
		message("Could not find FriBiDi. Disabling FriBiDi support.")
	endif()

	find_package( GoogleBreakpad )
	if(BREAKPAD_EXCEPTION_HANDLER_FOUND AND BREAKPAD_EXCEPTION_HANDLER_LIBRARIES)
	        add_definitions(-DHAVE_GOOGLE_BREAKPAD)

        	#include_directories( ${BREAKPAD_EXCEPTION_HANDLER_INCLUDE_DIR} )
                #SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${BREAKPAD_EXCEPTION_HANDLER_LIBRARIES})

	else()
		message("** WARNING: Could not find GoogleBreakpad. Disabling GoogleBreakpad support.")
	endif()

	#########################################################################################
	# megaglest lib

	SET(DIRS_WITH_SRC
                compression
		feathery_ftp	
		glew
		graphics
		graphics/gl
		lua
		map	
		platform/common
		sound
		sound/openal
		util
		xml)

	IF(WANT_STREFLOP)
		SET(DIRS_WITH_SRC
			${DIRS_WITH_SRC}
			streflop
			streflop/libm_flt32_source
                        streflop/softfloat)
	ENDIF()

	#IF(NOT WANT_STATIC_LIBS)
	#MESSAGE(STATUS "*** Searching for miniupnpc since WANT_STATIC_LIBS is off ...")
        MESSAGE(STATUS "*** Searching for miniupnpc ...")
	FIND_PACKAGE(Miniupnpc)
	#ENDIF()

	if(NOT MINIUPNP_FOUND)
		MESSAGE(STATUS "*** Using EMBEDDED miniupnpc since dev system does not have it... MINIUPNP_FOUND [${MINIUPNP_FOUND}]")

                if (NOT WIN32)
                   add_definitions (-DMINIUPNPC_SET_SOCKET_TIMEOUT)
                   add_definitions (-D_BSD_SOURCE)

                   if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
                      add_definitions (-DMACOSX -D_DARWIN_C_SOURCE)
                   else()
                      add_definitions (-D_POSIX_C_SOURCE=1)
                   endif ()
                endif()

		SET(DIRS_WITH_SRC ${DIRS_WITH_SRC} platform/miniupnpc)
	else()
		MESSAGE(STATUS "*** Using miniupnpc lib found in [${MINIUPNP_INCLUDE_DIR}] MINIUPNP_FOUND [${MINIUPNP_FOUND}] MINIUPNP_LIBRARY [${MINIUPNP_LIBRARY}]")

		INCLUDE_DIRECTORIES(${MINIUPNP_INCLUDE_DIR})
		SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${MINIUPNP_LIBRARY})
	endif()

	IF(MINIUPNPC_VERSION_PRE1_5)
	        ADD_DEFINITIONS(-DMINIUPNPC_VERSION_PRE1_5)
                message(STATUS "Adding macro for miniupnpc version: pre v1.5")
	ENDIF()
	IF(MINIUPNPC_VERSION_PRE1_6)
		ADD_DEFINITIONS(-DMINIUPNPC_VERSION_PRE1_6)
                message(STATUS "Adding macro for miniupnpc version: pre v1.6")
	ENDIF()
	IF(MINIUPNPC_VERSION_PRE1_7)
		ADD_DEFINITIONS(-DMINIUPNPC_VERSION_PRE1_7)
                message(STATUS "Adding macro for miniupnpc version: pre v1.7")
	ENDIF()

        IF(NOT MINIUPNPC_VERSION_PRE1_5 AND NOT MINIUPNPC_VERSION_PRE1_6 AND NOT MINIUPNPC_VERSION_PRE1_7)
                message(STATUS "**No macros required for miniupnpc version")
        ENDIF()

	IF(APPLE)
		SET(DIRS_WITH_SRC ${DIRS_WITH_SRC} platform/sdl)
		SET(DIRS_WITH_SRC ${DIRS_WITH_SRC} platform/posix)
		SET(DIRS_WITH_SRC ${DIRS_WITH_SRC} platform/macosx)
	ELSEIF(WIN32)
		SET(DIRS_WITH_SRC ${DIRS_WITH_SRC} platform/win32)
		SET(DIRS_WITH_SRC ${DIRS_WITH_SRC} ${PROJECT_SOURCE_DIR}/source/win32_deps/src)
	ELSE()
		SET(DIRS_WITH_SRC ${DIRS_WITH_SRC} platform/sdl)
		SET(DIRS_WITH_SRC ${DIRS_WITH_SRC} platform/posix)
		SET(DIRS_WITH_SRC ${DIRS_WITH_SRC} platform/unix)
	ENDIF()
	
	SET(MG_INCLUDES_ROOT "include/")
	SET(MG_SOURCES_ROOT "sources/")
	SET(MG_INCLUDE_FILES "")
	SET(MG_SOURCE_FILES "")
	INCLUDE_DIRECTORIES( ${MG_INCLUDES_ROOT}platform/sdl )
        INCLUDE_DIRECTORIES( ${MG_INCLUDES_ROOT}xml/rapidxml )
        INCLUDE_DIRECTORIES("sources/")

        option(DISABLE_IRCCLIENT "Disable libircclient support" OFF)

        IF(NOT DISABLE_IRCCLIENT)
	        #IF(NOT WANT_STATIC_LIBS)
		        MESSAGE(STATUS "*** Searching for libircclient since WANT_STATIC_LIBS is: ${WANT_STATIC_LIBS} ...")
		        FIND_PACKAGE(Ircclient)
	        #ENDIF()

	        if(NOT IRCCLIENT_FOUND)
		        MESSAGE(STATUS "*** Using EMBEDDED libircclient since this system does not have it... IRCCLIENT_FOUND [${IRCCLIENT_FOUND}]")

		        SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${PROJECT_SOURCE_DIR}/source/shared_lib/sources/libircclient/src/libircclient.c)
		        INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/source/shared_lib/include/libircclient/include)
	        else()
		        MESSAGE(STATUS "*** Using libircclient found in [${IRCCLIENT_INCLUDE_DIR}] IRCCLIENT_FOUND [${IRCCLIENT_FOUND}] IRCCLIENT_LIBRARY [${IRCCLIENT_LIBRARY}]")

		        INCLUDE_DIRECTORIES(${IRCCLIENT_INCLUDE_DIR})
		        SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${IRCCLIENT_LIBRARY})
	        endif()

                set(CMAKE_REQUIRED_INCLUDES "${IRCCLIENT_INCLUDE_DIR}")
                set(CMAKE_REQUIRED_LIBRARIES "${IRCCLIENT_LIBRARY}")
                include(CheckCXXSourceRuns)

                check_cxx_source_runs("
                #include <stdio.h>
                #include <stdlib.h>
                #include <libircclient.h>
                int main()
                {
                unsigned int high = 0;
                unsigned int low = 0;
                irc_get_version(&high,&low);
                if( (high == 1 && low >= 6) || (high > 1)) {
                  return 0;
                }
                throw 1;
                }"
                HAS_LIBIRCCLIENT_1_6)

                IF(HAS_LIBIRCCLIENT_1_6)
                        message(STATUS "Found HAS_LIBIRCCLIENT_1_6: ${HAS_LIBIRCCLIENT_1_6}")
                ELSE()
                        ADD_DEFINITIONS(-DLIBIRCCLIENT_PRE1_6)
                ENDIF()
        else()
                message(STATUS "****WARNING DISABLING libircclient!!")
                ADD_DEFINITIONS(-DDISABLE_IRCCLIENT)
        endif()

	IF(WIN32)
		SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${PROJECT_SOURCE_DIR}/source/shared_lib/sources/platform/posix/socket.cpp)
		SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${PROJECT_SOURCE_DIR}/source/shared_lib/sources/platform/posix/ircclient.cpp)
		SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${PROJECT_SOURCE_DIR}/source/shared_lib/sources/platform/posix/miniftpserver.cpp)
		SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${PROJECT_SOURCE_DIR}/source/shared_lib/sources/platform/posix/miniftpclient.cpp)
		SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${PROJECT_SOURCE_DIR}/source/shared_lib/sources/platform/sdl/gl_wrap.cpp)
		SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${PROJECT_SOURCE_DIR}/source/shared_lib/sources/platform/sdl/thread.cpp)
		SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${PROJECT_SOURCE_DIR}/source/shared_lib/sources/platform/sdl/window.cpp)
		SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${PROJECT_SOURCE_DIR}/source/shared_lib/sources/platform/sdl/window_gl.cpp)
		SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${PROJECT_SOURCE_DIR}/source/win32_deps/src/glprocs.c)
	ENDIF()
	
	FOREACH(DIR IN LISTS DIRS_WITH_SRC)
		set(INCLUDE_DIR_TO_GLOB ${MG_INCLUDES_ROOT}${DIR})
		INCLUDE_DIRECTORIES( ${INCLUDE_DIR_TO_GLOB} )
		IF(APPLE)
			#MESSAGE(STATUS "INCLUDE_DIR_TO_GLOB: ${INCLUDE_DIR_TO_GLOB}")
			FILE(GLOB INC_FILES_FROM_THIS_DIR ${INCLUDE_DIR_TO_GLOB}/*.h)
			#MESSAGE(STATUS "INCLUDE FILES GLOBBED: ${INC_FILES_FROM_THIS_DIR}")
			SET(MG_INCLUDE_FILES ${MG_INCLUDE_FILES} ${INC_FILES_FROM_THIS_DIR})
		ENDIF(APPLE)

		set(SRC_DIR_TO_GLOB ${MG_SOURCES_ROOT}${DIR})
		#MESSAGE(STATUS "SRC_DIR_TO_GLOB: ${SRC_DIR_TO_GLOB}")
		FILE(GLOB SRC_FILES_FROM_THIS_DIR ${SRC_DIR_TO_GLOB}/*.cpp)
		#MESSAGE(STATUS "SRC_FILES_FROM_THIS_DIR: ${SRC_FILES_FROM_THIS_DIR}")
		SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${SRC_FILES_FROM_THIS_DIR})
	
		set(SRC_DIR_TO_GLOB ${MG_SOURCES_ROOT}${DIR})
		#MESSAGE(STATUS "SRC_DIR_TO_GLOB: ${SRC_DIR_TO_GLOB}")
		FILE(GLOB SRC_FILES_FROM_THIS_DIR ${SRC_DIR_TO_GLOB}/*.c)
		#MESSAGE(STATUS "SRC_FILES_FROM_THIS_DIR: ${SRC_FILES_FROM_THIS_DIR}")
		SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${SRC_FILES_FROM_THIS_DIR})

		IF(APPLE)
			FILE(GLOB SRC_FILES_FROM_THIS_DIR ${SRC_DIR_TO_GLOB}/*.mm)
			#MESSAGE(STATUS "SRC_FILES_FROM_THIS_DIR: ${SRC_FILES_FROM_THIS_DIR}")
			SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${SRC_FILES_FROM_THIS_DIR})
			FILE(GLOB SRC_FILES_FROM_THIS_DIR ${SRC_DIR_TO_GLOB}/*.m)
			#MESSAGE(STATUS "SRC_FILES_FROM_THIS_DIR: ${SRC_FILES_FROM_THIS_DIR}")
			SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${SRC_FILES_FROM_THIS_DIR})	
		ENDIF(APPLE)
	ENDFOREACH(DIR)

	#MESSAGE(STATUS "Source files: ${MG_INCLUDE_FILES}")
	#MESSAGE(STATUS "Source files: ${MG_SOURCE_FILES}")
	#MESSAGE(STATUS "Include dirs: ${INCLUDE_DIRECTORIES}")

	IF(APPLE)
		SET(PLATFORM_SPECIFIC_DEFINES "-DHAVE_SYS_IOCTL_H")
	ELSEIF(WIN32)
		SET(PLATFORM_SPECIFIC_DEFINES "-DX11_AVAILABLE")
	ELSE()
		SET(PLATFORM_SPECIFIC_DEFINES "-DX11_AVAILABLE -DHAVE_SYS_IOCTL_H")
	ENDIF()

	SET_SOURCE_FILES_PROPERTIES(${MG_SOURCE_FILES} PROPERTIES COMPILE_FLAGS 
		"${PLATFORM_SPECIFIC_DEFINES} ${STREFLOP_PROPERTIES} ${CXXFLAGS}")
	
	SET_SOURCE_FILES_PROPERTIES(${MG_INCLUDE_FILES} PROPERTIES HEADER_FILE_ONLY 1)

	IF(NOT WIN32)
		ADD_LIBRARY(${TARGET_NAME} STATIC ${MG_SOURCE_FILES} ${MG_INCLUDE_FILES})
		#MESSAGE(STATUS "Building shared game library as a STATIC library and saving to [${LIBRARY_OUTPUT_PATH}]")
	ELSE()
		SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/data/glest_game)
		ADD_LIBRARY(${TARGET_NAME} STATIC ${MG_SOURCE_FILES} ${MG_INCLUDE_FILES})
		MESSAGE(STATUS "Building shared game library as a SHARED library and saving to [${LIBRARY_OUTPUT_PATH}]")
	ENDIF()

	# ADD_LIBRARY(libmegaglest SHARED ${MG_SOURCE_FILES} ${MG_INCLUDE_FILES})
	# ADD_LIBRARY(libmegaglest STATIC ${MG_SOURCE_FILES} ${MG_INCLUDE_FILES})

	MESSAGE(STATUS "EXTERNAL_LIBS = [${EXTERNAL_LIBS}]")

	OPTION(BUILD_MEGAGLEST_UPNP_DEBUG "Build MegaGlest with UPNP DEBUG enabled" OFF)
	IF(BUILD_MEGAGLEST_UPNP_DEBUG)
		MESSAGE(STATUS "Will try to build MegaGlest shared library with DEBUG info for UPNP")
		ADD_DEFINITIONS("-DDEBUG")
	ENDIF()

	IF(WIN32)
	  TARGET_LINK_LIBRARIES(${TARGET_NAME} stdc++ gcc odbc32 wsock32 winspool winmm shell32 comctl32 ctl3d32  advapi32 wsock32 opengl32 glu32 ole32 oleaut32 uuid mingw32 ddraw dsound dxguid ws2_32 iphlpapi wsock32 libogg libvorbis libvorbisfile zlib jpeg libpng xerces-c2_8_0 OpenAL32 libcurl winmm gdi32 opengl32 glu32 SDL SDLmain lua5.1 stdc++ moldname mingwex msvcrt user32 kernel32)

	  IF(WANT_STREFLOP)
	    TARGET_LINK_LIBRARIES(${TARGET_NAME} streflop)
          ENDIF()
	  TARGET_LINK_LIBRARIES(${TARGET_NAME} ${EXTERNAL_LIBS})
	ELSE()
          IF(WANT_STREFLOP)
	    TARGET_LINK_LIBRARIES(${TARGET_NAME} streflop)
 	  ENDIF()
	  TARGET_LINK_LIBRARIES(${TARGET_NAME} ${EXTERNAL_LIBS})

	ENDIF()

	#IF(BUILD_MEGAGLEST_UPNP_DEBUG)
		# REMOVE_DEFINITIONS("-DDEBUG")
	#ENDIF()

ENDIF()
